package com.erp4j.dynamicfield.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.erp4j.dynamicfield.entity.Order;
import com.erp4j.dynamicfield.service.OrderService;
import com.erp4j.dynamicfield.vo.QueryParam;
import com.erp4j.dynamicfield.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 分页查询订单数据
     */
    @PostMapping("/query/{moduleId}")
    public Result<IPage<Order>> queryOrders(
            @PathVariable Long moduleId,
            @RequestBody List<QueryParam> params,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(orderService.queryByParams(moduleId, params, pageNum, pageSize));
    }

    /**
     * 根据ID获取订单
     */
    @GetMapping("/{id}")
    public Result<Order> getOrder(@PathVariable Long id) {
        return Result.success(orderService.getById(id));
    }

    /**
     * 保存或更新订单
     */
    @PostMapping
    public Result<Order> saveOrder(@RequestBody Order order) {
        orderService.saveOrUpdateOrder(order);
        return Result.success(order);
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteOrder(@PathVariable Long id) {
        orderService.removeById(id);
        return Result.success();
    }
}
